﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.UserControl;

using System.Data.Linq;
using System.Collections.Generic;
using System.Text;


namespace Hisun.View.PIPDeliverInfo
{
    public partial class PIPDiliverInfoSelectCenter : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string script;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //CreateTree();
                CenterId.Value = Request.QueryString["centerid"].ToString();
                PIPid.Value = Request.QueryString["pipid"].ToString();

                var MyProductID = (from m in db.TABC_JS_ProductInProcess
                                   where m.作业中心ID == Convert.ToInt32(CenterId.Value)
                                   && m.中间体ID == PIPid.Value
                                   select m.产品ID).Single();

                var GoalCenterAndPIP = from n in db.TABC_JS_ProductInProcess
                                       join a in db.TABC_JS_CostCenter on n.作业中心ID equals a.作业中心ID
                                        where n.产品ID == MyProductID
                                       && n.作业中心ID != Convert.ToInt32(CenterId.Value)
                                       && n.中间体ID != PIPid.Value 
                                       && a.类型.StartsWith("B")
                                       && n.可用 == true
                                       select new 
                                       {
                                      目标作业中心ID = n.作业中心ID,
                                      目标作业中心名称 = a.作业中心名称,
                                      目标中间体ID = n.中间体ID,
                                      目标中间体名称 = n.中间体名称,
                                       n.计量单位
                                       };

                    if (GoalCenterAndPIP.Count() == 0)
                    {

                        Response.Write("<script>alert('当前作业中心的中间体产品没有下一个环节'); </script>");
                    }
                    else 
                    {

                        GridView_GoalCenterAndPIPInfo.DataSource = GoalCenterAndPIP;
                        GridView_GoalCenterAndPIPInfo.DataBind();
                    }

            }

        }


        protected void GridView_GoalPIPInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            string CenterInfo = GridView_GoalCenterAndPIPInfo.SelectedRow.Cells[0].Text.ToString() + "&"
                                        + GridView_GoalCenterAndPIPInfo.SelectedRow.Cells[1].Text.ToString() + "&"
                                        + GridView_GoalCenterAndPIPInfo.SelectedRow.Cells[2].Text.ToString() + "&"
                                        + GridView_GoalCenterAndPIPInfo.SelectedRow.Cells[3].Text.ToString() + "&"
                                        + GridView_GoalCenterAndPIPInfo.SelectedRow.Cells[4].Text.ToString();
            //作业中心ID，作业中心名称，中间体ID ，中间体名称，计量单位

 

            StringBuilder s = new StringBuilder();
            s.Append("<script language=javascript>" + "\n");
            s.Append("window.returnValue='" + CenterInfo.Trim() + "';" + "\n");
            s.Append("window.close();" + "\n");
            s.Append("</script>");
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            string csname = "ltype";
            if (!cs.IsStartupScriptRegistered(cstype, csname))
                cs.RegisterStartupScript(cstype, csname, s.ToString());
        }


    }
}
